const db = wx.cloud.database();
const _ = db.command;
const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    swiperImgURLs: ['../../images/naruto.png', '../../images/sai.png', '../../images/sasuki.png', '../../images/sakura.png'],
    selected: 0,
    userInfo: null,
    teamList: [],
    statusBarHeight: app.globalData.statusBarHeight,
    navbarHeight: app.globalData.navbarHeight,
    typeMap: app.globalData.typeMap
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    let app = getApp();
    // 获得用户信息（全局初始化）
    wx.showLoading({
      title: '等待全局加载',
      mask: true
    });
    console.log('home_page onload init');
    var openid = await app.updateOpenid();
    var userInfo = await app.updateUserInfo();
    wx.hideLoading();
    // 改写AppData，页面保存信息
    this.setData({
      userInfo: userInfo
    });
    //初始化队伍列表
    this.initTeamList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
    let app = getApp()
    this.setData({
      userInfo: app.globalData.userInfo
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  changeSelected: function (e) {
    var index = e.currentTarget.dataset.index;
    var url = '';
    switch (Number(index)) {
      case 0:
        url = '../postIdea/postIdea';
        break;
      case 1:
        url = '../launchProj/launchProj';
        break;
      default:
        url = '../cases/cases';
        break;
    }
    wx.navigateTo({
      url: url
    });
  },

  toAdd: function () {
    var curSection = this.data.selected;
    if (this.data.userInfo == null) {
      wx.switchTab({
        url: '../personal/personal'
      });
    } else {
      wx.navigateTo({
        url: '../initiat_project/initiat_project?section=' + String(curSection)
      });
    }
  },

  initTeamList: function () {
    let that = this;
    wx.cloud.callFunction({
      name: 'getPostList',
      data: {
        skip: 0,
        type: 'team'
      }
    }).then((res) => {
      console.log('team list', res);
      that.data.teamList = [];
      for (var i in res.result) {
        that.data.teamList.push({
          title: res.result[i].name,
          date: res.result[i].date,
          brief: res.result[i].content,
          ownerName: res.result[i].owner[0].nickName,
          ownerAvatar: res.result[i].owner[0].avatarUrl,
          type: res.result[i].type,
          imgUrl: res.result[i].imgUrl,
          id: res.result[i]._id
        });
      }
      that.setData({
        teamList: that.data.teamList
      });
    }).catch((res) => {
      console.log('error: team list');
    })
  },
  /**
   * 转到队伍详情页面
   * @param {*} arg tap事件传入的参数
   */
  toArticle: function (arg) {
    console.log('tap article', arg);
    wx.navigateTo({
      url: '../article/index?type=team&id=' + arg.currentTarget.dataset.id,
    });
  }
})